#include <string.h>

#include "lib.h"

#include "crround.h"
#include "crbet.h"

#define MAX_BETS	CrapsBet::NUMBETS

CrapsRound::CrapsRound(void)
{
	m_numBets = 0;

	memset(m_bets, 0, sizeof(CrapsBet*) * MAX_BETS);
}

CrapsRound::~CrapsRound(void)
{
	int32 i;

	for (i = 0; i < MAX_BETS; i++)
		delete m_bets[i];
}

void
CrapsRound::updateBetAmounts(int32 *bets)
{
	/*
	for (int32 i=0; i < MAX_BETS; i++)
	{
	// update each bet amount with the value passed back from
	// the server. Since negative values indicate bets that continue
	// we take the absolute value
	if (m_bets[i])
	m_bets[i]->setAmount(ABS(bets[i]));
	}
	*/
}

int32 *
CrapsRound::getBetAmounts()
{
	int32 *betAmounts = new int32[CrapsBet::NUMBETS];

	for (int32 i=0; i < MAX_BETS; i++)
	{
		if (m_bets[i])
			betAmounts[i] = m_bets[i]->getAmount();
		else
			betAmounts[i] = 0;
	}

	return betAmounts;
}

uint8 
CrapsRound::numMoneyBets()
{
	int32 i;
	uint8 numBets = 0;
	CrapsBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];
		if (bet && (bet->getAmount() > 0))
			numBets++;
	}

	return numBets;
}

int32
CrapsRound::totalBet(void)
{
	int32 i;
	int32 total = 0;
	int32 amount = 0;
	CrapsBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];
		if (bet)
		{
			// Fogbugz 19771 -- Only include positive bets in the total.  This
			// parallels the logic in numMoneyBets. This should prevent an invalid
			// 'negative' bet from ever causing the total bets in crapsgames to differ
			// from the sum of the individual bets in crapsbets.
			amount = bet->getAmount();
			if ( amount > 0 )
			{
				total += bet->getAmount();
			}
		}
	}

	return total;
}


int32
CrapsRound::totalBonusBet(void)
{
	int32 i;
	int32 total = 0;
	CrapsBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];
		if (bet)
			total += bet->getBonusAmount();
	}

	return total;
}



//
// given a point, tableLimits, and round
// this function determines whether there is at least
// one bet that can be validly placed.
//
// bets that are dependent on others can only be placed
// if the others have positive bet amounts -- therefore,
// to repeat a previous dependant bet, we must have the
// round containing the 'depends on' bet
//
bool8
CrapsRound::repeatableBets(uint8 point, CTableLimits &tableLimits, CrapsRound *dependsRound)
{
	CrapsBet *bet;
	CrapsBet *depends;
	int32	  amount = 0;
	int32	  betMin, betMax;

	for (int32 i = 0; i < MAX_BETS; i++)
	{
		bet			= getBet((uint8)i);
		depends		= dependsRound->getBet((uint8)i)->getDepends();

		if (bet)
			amount = bet->getAmount();

		betMax = tableLimits.getBetMax((uint8)i);
		betMin = tableLimits.getBetMin((uint8)i);

		if	(bet && (amount > 0) && (bet->getLimit(point, betMax ) > 0) &&
			( (!depends || (depends && (depends->getAmount() > 0)  )) && 
			( (amount <= betMax) && (amount >= betMin) )
			)
			)
			return true;
	}

	return false;
}

//
// given a point,
// this function determines whether there is at least
// one bet that can be removed
//
bool8
CrapsRound::removableBets(uint8 point)
{
	CrapsBet *bet;

	for (int32 i = 0; i < MAX_BETS; i++)
	{
		bet = getBet((uint8)i);
		if (bet && (bet->getAmount() > 0) && (bet->canRemove(point)) )
			return true;
	}

	return false;
}


bool8 
CrapsRound::addBet(CrapsBet *bet)
{
	if (bet && m_numBets < MAX_BETS && !m_bets[bet->getBetID()])
	{
		m_bets[bet->getBetID()] = bet;
		m_numBets++;
		return true;
	}
	else
		return false;
}

int32
CrapsRound::calcPayout(uint8 die1, uint8 die2) 
{
	int32 i, payout;
	CrapsBet *bet;

	// Payout includes original bet
	//
	payout = 0;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = getBet((uint8)i);
		if(bet)
			payout += CalcBetPayout(die1, die2) + bet->getAmount();
	}

	return payout;
}

int32
CrapsRound::calcTotalWinningBets(uint8 die1, uint8 die2)
{
	int32 i, total;
	CrapsBet *bet;

	total = 0;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = getBet((uint8)i);
		if(bet) total += bet->getAmount();
	}

	return total;
}

int32 
CrapsRound::CalcBetPayout(uint8 die1, uint8 die2)
{
	return 0;
}



// CGC: CSN-2224
int32
CrapsRound::getMaxLimit(uint8 point, CTableLimits &tableLimits)
{
	CrapsBet *bet;
	int32	  betMax = 0;

	for (int32 i = 0; i < MAX_BETS; i++)
	{
		bet			= getBet((uint8)i);
		if (bet)
		{
			int betMaxAux = bet->getLimit(point, tableLimits.getBetMax((uint8)i));
			if (betMaxAux > betMax)
				betMax = betMaxAux;
		}
	}

	return betMax;
}



